package cn.hjh.rpc.consumer;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

@Service
public class FixRpcMsgListener implements MessageListener
{
	private RabbitTemplate amqpTemplate;

	public void onMessage(Message message)
	{
		String msg = new String(message.getBody());
		String json = "【 " + msg.substring(msg.indexOf("|") + 1) + "】rpc 请求！";
		MessageProperties msgProp = message.getMessageProperties();
		
		String replyTo = (String) msgProp.getHeaders().get("replyTo");
		System.out.println("replyTo = " + replyTo);
		System.out.println("当前处理线程名称 : " + Thread.currentThread().getName());
		
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setCorrelationId(message.getMessageProperties().getCorrelationId());
		messageProperties.setContentType(message.getMessageProperties().getContentType());
		
		Message responseMsg = new Message(json.getBytes(),messageProperties);
//		Message responseMsg = new Message(json.getBytes(),message.getMessageProperties());
		//"jcToRmpsKey"
//		try
//		{
//			Thread.sleep(6000);
//		} catch (Exception e)
//		{
//		}
		amqpTemplate.send(replyTo, responseMsg);
	}

	public RabbitTemplate getAmqpTemplate()
	{
		return amqpTemplate;
	}

	public void setAmqpTemplate(RabbitTemplate amqpTemplate)
	{
		this.amqpTemplate = amqpTemplate;
	}

}
